package cn.edu.neusoft.zengweidong707.neufood.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.activity.FoodDetailsActivity;
import cn.edu.neusoft.zengweidong707.neufood.activity.FoodListActivity;
import cn.edu.neusoft.zengweidong707.neufood.bean.UserCollectionListBean;

/**
 * Created by Lucio on 2017/12/12.
 */

public class CollectAdapter extends BaseAdapter {
    public CollectAdapter(Context context, List<UserCollectionListBean> items) {
        super(context, items);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.items_collect, parent, false);
        CollectListViewHolder collectListViewHolder = new CollectListViewHolder(itemView);
        return collectListViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final UserCollectionListBean entity = (UserCollectionListBean) items.get(position);
        if (null == entity)
            return;
        CollectListViewHolder myViewHolder = (CollectListViewHolder) holder;
        if( 0 == entity.getFlag())
        {
            String photo = entity.getPic();
            if( photo == "" ){
                myViewHolder.pic.setImageResource(R.mipmap.ic_launcher);
            }
            else{
                Picasso.with(context)
                        .load(photo)
                        .into(myViewHolder.pic);
            }
            myViewHolder.name.setText(entity.getShopname());
            myViewHolder.address.setText(entity.getAddress());
        }
        else
        {
            String photo = entity.getPic();
            if( photo == "" ){
                myViewHolder.pic.setImageResource(R.mipmap.ic_launcher);
            }
            else{
                Picasso.with(context)
                        .load(photo)
                        .into(myViewHolder.pic);
            }
            myViewHolder.name.setText(entity.getFoodname());
            Double prices = entity.getPrice();
            myViewHolder.price.setText(prices+"元");
        }

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( 0 == entity.getFlag())
                {
                    int shopid = entity.getShop_id();
                    String shopname = entity.getShopname();
                    Intent intent = new Intent(context, FoodListActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("shopid",shopid);
                    bundle.putString("shopname",shopname);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
                else
                {
                    int foodid = entity.getFood_id();
                    Intent intent = new Intent(context, FoodDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("foodid",foodid);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }

            }
        });
    }

    public class CollectListViewHolder extends RecyclerView.ViewHolder{
        ImageView pic;
        TextView name;
        TextView price;
        TextView address;

        public CollectListViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.cl_name);
            pic = (ImageView) itemView.findViewById(R.id.cl_pic);
            price = (TextView) itemView.findViewById(R.id.cl_price);
            address = (TextView) itemView.findViewById(R.id.cl_address);
        }
    }
}
